package com.whccb.apps.office.document.formatter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.client.notification.NotificationMessageFormatter;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.whccb.apps.office.document.ODConstant;

public class ODNotificationFormatter implements NotificationMessageFormatter {
    /**
     * 收文流程，行长、董事长签批后，给承办部门经理发送通知。
     * 
     * @param user
     *            通知查看人
     * @param content
     *            发送的原始内容
     * @return ResponseObject，包含content和buttons两个变量
     */
    @Override
    public ResponseObject parser(UserContext userContext, String content) {

        ResponseObject ro = ResponseObject.newOkResponse();
        JSONObject contentJO = JSONObject.parseObject(content);
        String bindid = contentJO.getString("bindid");
        int state = 2;
        if (contentJO.containsKey("state")) {
            try {
                state = Integer.parseInt(contentJO.get("state").toString());
            } catch (Exception localException) {

            }
        }
        String taskid = contentJO.getString("taskid");
        String notifycontent = contentJO.getString("notifycontent");
        ro.put("content", notifycontent);
        List<Map<String, String>> buttons = new ArrayList<Map<String, String>>();
        Map<String, String> button = new HashMap<String, String>();
        button.put("name", "查看流程");
        button.put("target", "mainFrame");// 新窗口，不常用。只允许四个常量：_blank/mainFrame/ajax/app

        button.put("action",
                SDK.getFormAPI().getFormURL("", userContext.getSessionId(),
                        bindid, taskid, state, "", "", "", false));

        button.put("color", "blue");// 只允许三个常量：blue/white/red
        button.put("functionId",
                SDK.getAppAPI().getAppContext(ODConstant.APP_ID).getId());
        button.put("functionName", "查看流程");
        buttons.add(button);
        ro.put("buttons", buttons);
        return ro;
    }
}
